package com.example.electronicmall.service.impl;

import com.example.electronicmall.dao.OrderDao;
import com.example.electronicmall.entity.*;
import com.example.electronicmall.service.*;
import com.example.electronicmall.to.BusinessOrderTo;
import com.example.electronicmall.to.ItemCountTo;
import com.example.electronicmall.to.OrderDetailTo;
import com.example.electronicmall.to.OrderTo;
import com.example.electronicmall.utils.PageUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


@Service
public class OrderServiceImpl extends ServiceImpl<OrderDao, OrderEntity> implements OrderService {

    @Autowired
    OrderDetailService orderDetailService;

    @Autowired
    BusinessShopService businessShopService;

    @Autowired
    OrderDao orderDao;

    @Autowired
    ShopService shopService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        return null;
    }

    @Override
    public String createOrder(OrderTo orderTo) {
        if (orderTo == null) {
            return "";
        }
        //1.检查当前用户是否在当前门店有未支付的订单
        OrderEntity preOrder = checkNoPayOrder(orderTo.getUserId(), orderTo.getShopId());
        if(preOrder == null){
            Integer userId = orderTo.getUserId();
            OrderEntity order = new OrderEntity();
            String orderId = UUID.randomUUID().toString();
            order.setOrderId(orderId);
            order.setUserId(userId);
            order.setOrderPrice(orderTo.getTotalPrice());
            Integer shopId = orderTo.getShopId();
            Integer businessId = businessShopService.getBaseMapper().selectOne(new QueryWrapper<BusinessShopEntity>().eq("shop_id", shopId)).getBusinessId();
            order.setBusinessId(businessId);
            order.setIsPayed(0);
            Date date = new Date();
            order.setCreatedAt(date);
            order.setUpdatedAt(date);
            order.setIsDeleted(0);
            order.setShopId(shopId);
            orderDao.save(order);
            insertOrderDetails(order, orderTo);
            return orderId;
        }else{
            //合并订单
            preOrder.setUpdatedAt(new Date());
            preOrder.setOrderPrice(preOrder.getOrderPrice().add(orderTo.getTotalPrice()));
            System.out.println(preOrder.getOrderPrice());
            insertOrderDetails(preOrder, orderTo);
            orderDao.updateOrderPrice(preOrder);
            return "-1";
        }
    }

    public void insertOrderDetails(OrderEntity order, OrderTo orderTo){
        for (ItemCountTo i : orderTo.getItemList()) {
            OrderDetailEntity od = new OrderDetailEntity();
            od.setOrderId(order.getOrderId());
            od.setItemId(i.getId());
            od.setQuantity(i.getQuantity());
            orderDetailService.getBaseMapper().insert(od);
        }
    }

    //查询当前用户在当前门店是否有未支付的订单
    public OrderEntity checkNoPayOrder(Integer userId, Integer shopId){
        return orderDao.checkNoPayOrder(userId, shopId);
    }

    @Override
    public List<OrderDetailTo> listOrdersByUserId(Integer userId) {
        List<OrderEntity> orderList = orderDao.listOrdersByUserId(userId);
        return orderList.stream().map(v -> {
            OrderDetailTo orderDetailTo = new OrderDetailTo();
            orderDetailTo.setOrderId(v.getOrderId());
            orderDetailTo.setShopId(v.getShopId());
            orderDetailTo.setTotalPrice(v.getOrderPrice());
            orderDetailTo.setIsPayed(v.getIsPayed());
            orderDetailTo.setIsDeleted(v.getIsDeleted());
            return orderDetailTo;
        }).collect(Collectors.toList());
    }

    @Override
    public List<BusinessOrderTo> listOrdersByBusinessId(Integer businessId) {
        List<OrderEntity> orders = orderDao.listOrdersByBusinessId(businessId);
        List<BusinessOrderTo> businessOrderTos = orders.stream().map(v -> {
            BusinessOrderTo businessOrderTo = new BusinessOrderTo();
            BeanUtils.copyProperties(v, businessOrderTo);
            businessOrderTo.setOrderPrice(v.getOrderPrice());
            //查询门店名称
            ShopEntity shop = shopService.getBaseMapper().selectOne(new QueryWrapper<ShopEntity>().eq("shop_id", v.getShopId()));
            businessOrderTo.setShopName(shop.getShopName());
            return businessOrderTo;
        }).collect(Collectors.toList());
        return businessOrderTos;
    }

    @Override
    public void receiveOrder(String orderId) {
        OrderEntity preOrder = orderDao.queryOrderByOrderId(orderId);
        preOrder.setUpdatedAt(new Date());
        preOrder.setIsPayed(2);
        orderDao.updateOrderByOrderId(preOrder);
    }

    @Override
    public void rejectOrder(String orderId) {
        OrderEntity preOrder = orderDao.queryOrderByOrderId(orderId);
        preOrder.setUpdatedAt(new Date());
        preOrder.setIsPayed(3);
        orderDao.updateOrderByOrderId(preOrder);
    }

    @Override
    public void payOrder(String orderId) {
        OrderEntity preOrder = orderDao.queryOrderByOrderId(orderId);
        preOrder.setUpdatedAt(new Date());
        preOrder.setIsPayed(1);
        orderDao.updateOrderByOrderId(preOrder);
    }

    @Override
    public void deleteOrder(String orderId) {
        OrderEntity preOrder = orderDao.queryOrderByOrderId(orderId);
        preOrder.setUpdatedAt(new Date());
        preOrder.setIsDeleted(1);
        orderDao.deleteOrderByOrderId(preOrder);
    }

}